package bw.gui;

import bw.owl.Ontology;
import bw.owl.Umiejetnosc;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntologyChange;

public class UmiejetnosciPanel extends JPanel implements ActionListener{

    private InstanceViewer parent;
    private GroupLayout layout;
    private JButton dodaj;
//    private JButton usun;
//    private JButton zmienNazwe;
    private JList listaUmiejetnosci;

    public UmiejetnosciPanel(InstanceViewer viewer) {
	parent = viewer;
        layout = new GroupLayout(this);
        setLayout(layout);

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        listaUmiejetnosci = new JList();
        JScrollPane scrollPane = new JScrollPane(listaUmiejetnosci);
        scrollPane.setMinimumSize(new Dimension(600, 100));

        dodaj = new JButton("Dodaj");
//        usun = new JButton("Usuń");
//        zmienNazwe = new JButton("Zmień nazwę");

        layout.setHorizontalGroup(
                layout.createParallelGroup()
                .addComponent(scrollPane)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(dodaj)
//                    .addComponent(usun)
//                    .addComponent(zmienNazwe)
                    ));

        layout.setVerticalGroup(
                layout.createSequentialGroup()
                .addComponent(scrollPane)
                .addGroup(layout.createParallelGroup()
                    .addComponent(dodaj)
//                    .addComponent(usun)
//                    .addComponent(zmienNazwe)
                    ));

        dodaj.addActionListener(this);
//        usun.addActionListener(this);
//        zmienNazwe.addActionListener(this);
    }

    void setUmiejetnosci(ArrayList<Umiejetnosc> umiejetnosci) {
        listaUmiejetnosci.setListData(umiejetnosci.toArray());
    }

    

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == dodaj){
            dodaj();
	}
//        else if(e.getSource() == usun){
//            usun();
//        }

    }
    
    private void dodaj() {
        final JFrame frame = new JFrame("Nazwa umiejętności:");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        final JTextField field = new JTextField(20);
        frame.add(field);
        JButton akceptuj = new JButton("OK");
        frame.add(akceptuj);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

	    akceptuj.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
		    OWLNamedIndividual ind = Ontology.getFactory()
			    .getOWLNamedIndividual(
				    IRI.create(Ontology.getOwlString() + "#" +
					    field.getText()));

		    List<OWLOntologyChange> oc = new ArrayList<OWLOntologyChange>();

		    OWLClass klasaUmiejetnosci = Ontology.getFactory()
			    .getOWLClass(
				    IRI.create(Ontology.getOwlString()
					    + "#Umiejetnosc"));
		    OWLAxiom axiom = Ontology.getFactory()
			    .getOWLClassAssertionAxiom(klasaUmiejetnosci, ind);
		    oc.addAll(Ontology.getManager().addAxiom(
			    Ontology.getOntology(), axiom));

		    Ontology.getManager().applyChanges(oc);
		    Ontology.reload();
		    parent.update();

		    frame.dispose();
		}
	    });
    }
}
